package cs4280.action;

import cs4280.bean.*;
import cs4280.dao.*;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import control.*;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;

public class CinemaControl extends HttpServlet {

    public static boolean userLoggedin = false;
    public static Member member;
    public static HttpSession session;

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //   String jspPage = this.doIndexPage(request, response);

        // this.dispatchJsp(jspPage, request, response);
        this.doPost(request, response);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CounterDAO counterDAO=new CounterDAO();
        Counter counter=counterDAO.getCounter();
        request.setAttribute("counter", counter);

        String jspPage = new String("");
        if ("addmovie".equalsIgnoreCase(request.getParameter("todo")) != false) {
            jspPage = this.doPrepareAddMoviePage(request, response);
        } else if ("doaddmovie".equalsIgnoreCase(request.getParameter("todo")) != false) {
            jspPage = this.doAddMoviePage(request, response);
        } else if ("deletemovie".equalsIgnoreCase(request.getParameter("todo")) != false) {
            jspPage = this.doPrepareDeleteMoviePage(request, response);
        } else if ("dodeletemovie".equalsIgnoreCase(request.getParameter("todo")) != false) {
            jspPage = this.doDeleteMoviePage(request, response);
        } else if ("updatemovie".equalsIgnoreCase(request.getParameter("todo")) != false) {
            jspPage = this.doPrepareUpdateMoviePage(request, response);
        } else if ("doupdatemovie".equalsIgnoreCase(request.getParameter("todo")) != false) {
            jspPage = this.doUpdateMoviePage(request, response);
        } else if ("register".equalsIgnoreCase(request.getParameter("todo")) != false) {
            jspPage = this.doPrepareRegisterPage(request, response);
        } else if ("doregister".equalsIgnoreCase(request.getParameter("todo")) != false) {
            jspPage = this.doRegister(request, response);
        } else if ("updatemember".equalsIgnoreCase(request.getParameter("todo")) != false) {
            jspPage = this.doPrepareUpdateMemberPage(request, response);
        } else if ("doupdatemember".equalsIgnoreCase(request.getParameter("todo")) != false) {
            jspPage = this.doUpdateMemberPage(request, response);
        } else if ("movieviewer".equalsIgnoreCase(request.getParameter("todo")) != false) {
            jspPage = this.doMovieViewer(request, response);
        } else if ("doSort".equalsIgnoreCase(request.getParameter("todo")) != false) {
            jspPage = this.doSortMovie(request, response);
        } else if ("doSearch".equalsIgnoreCase(request.getParameter("todo")) != false) {
            jspPage = this.doSearchMovie(request, response);
        } else if ("bookticket".equalsIgnoreCase(request.getParameter("todo")) != false) {
            jspPage = this.bookTicket(request, response);
        } else if ("dopay".equalsIgnoreCase(request.getParameter("todo")) != false) {
            jspPage = this.doPay(request, response);
        } else if ("dobookticket".equalsIgnoreCase(request.getParameter("todo")) != false) {
            jspPage = this.doBookTicket(request, response);
        } else if ("doauthenticate".equalsIgnoreCase(request.getParameter("todo")) != false) {
            jspPage = this.doAuthenticate(request, response);
        } else if ("domanage".equalsIgnoreCase(request.getParameter("todo")) != false) {
            jspPage = this.doManage(request, response);
        } else if ("logout".equalsIgnoreCase(request.getParameter("todo")) != false) {
            jspPage = this.doLogout(request, response);
        } else if ("doindex".equalsIgnoreCase(request.getParameter("todo")) != false) {
            jspPage = this.doIndexPage(request, response);
        } else if ("domanagement".equalsIgnoreCase(request.getParameter("todo")) != false) {
            jspPage = this.doPrepareManagement(request, response);
        } /*else if ("applyrefund".equalsIgnoreCase(request.getParameter("todo")) != false) {
            jspPage = this.applyRefund(request, response);
        } */else if ("doapplyrefund".equalsIgnoreCase(request.getParameter("todo")) != false) {
            jspPage = this.doApplyRefund(request, response);
        } else if ("domanageRefund".equalsIgnoreCase(request.getParameter("todo")) != false) {
            jspPage = this.doManageRefund(request, response);
        } else if ("reserveseat".equalsIgnoreCase(request.getParameter("todo")) != false) {
            jspPage = this.reserveSeat(request, response);
        } else if ("releaseseat".equalsIgnoreCase(request.getParameter("todo")) != false) {
            jspPage = this.releaseSeat(request, response);
        } else if ("doReserve".equalsIgnoreCase(request.getParameter("todo")) != false) {
            jspPage = this.doReserve(request, response);
        } else if ("doRelease".equalsIgnoreCase(request.getParameter("todo")) != false) {
            jspPage = this.doRelease(request, response);
        } else if ("messageBox".equalsIgnoreCase(request.getParameter("todo")) != false) {
            jspPage = this.doMessageBox(request, response);
        } else if ("addMessage".equalsIgnoreCase(request.getParameter("todo")) != false) {
            jspPage = this.doAddMessage(request, response);
        } else if ("deleteMessage".equalsIgnoreCase(request.getParameter("todo")) != false) {
            jspPage = this.doDeleteMessage(request, response);
        }else if ("doviewstatistic".equalsIgnoreCase(request.getParameter("todo")) != false) {
            jspPage = this.doViewStatistics(request, response);
        } else if("doupdateUser".equalsIgnoreCase(request.getParameter("todo"))!=false) {
            jspPage=this.doUpdateUser(request,response);
        } else if("doSortManager".equalsIgnoreCase(request.getParameter("todo"))!=false) {
            jspPage=this.doSortManager(request,response);
        } else if("doSearchManager".equalsIgnoreCase(request.getParameter("todo"))!=false) {
            jspPage=this.doSearchManager(request,response);
        }else if("deletemember".equalsIgnoreCase(request.getParameter("todo"))!=false) {
            jspPage=this.doPrepareDeleteMember(request,response);
        }else if("dodeletemember".equalsIgnoreCase(request.getParameter("todo"))!=false) {
            jspPage=this.doDeleteMember(request,response);
        }else if("managescreening".equalsIgnoreCase(request.getParameter("todo"))!=false) {
            jspPage=this.doPrepareScreeningManagement(request,response);
        }else if("addscreening".equalsIgnoreCase(request.getParameter("todo"))!=false) {
            jspPage=this.doPrepareAddScreening(request,response);
        }else if("doaddscreening".equalsIgnoreCase(request.getParameter("todo"))!=false) {
            jspPage=this.doAddScreening(request,response);
        }else if("updatescreening".equalsIgnoreCase(request.getParameter("todo"))!=false) {
            jspPage=this.doPrepareUpdateScreening(request,response);
        }else if("doupdatescreening".equalsIgnoreCase(request.getParameter("todo"))!=false) {
            jspPage=this.doUpdateScreening(request,response);
        }else if("deletescreening".equalsIgnoreCase(request.getParameter("todo"))!=false) {
            jspPage=this.doPrepareDeleteScreening(request,response);
        }else if("dodeletescreening".equalsIgnoreCase(request.getParameter("todo"))!=false) {
            jspPage=this.doDeleteScreening(request,response);
        }




        else {
            jspPage = this.doIndexPage(request, response);
        }

        this.dispatchJsp(jspPage, request, response);
    }



    private void dispatchJsp(String jspPage, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher(jspPage).forward(request, response);
    }

    private String doAuthenticate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        session = request.getSession();
        loginControll login = new loginControll();
        return login.doAuthenticate(request, response);
    }
/*
    private String applyRefund(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        requestControll requestC = new requestControll();
        requestC.prepareRefund(request, response);
        return "/WEB-INF/jsp/applyRefund.jsp";
    }*/

    private String doApplyRefund(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        requestControll requestC = new requestControll();
        requestC.doRefund(request, response);
        requestC.prepareRefund(request, response);
        return "/WEB-INF/jsp/manageMember.jsp";
    }

    private String doManageRefund(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        requestControll requestC = new requestControll();
        String actionRefund = request.getParameter("refundAction");
        if (request.getParameter("bookingid") != null) {
            if (actionRefund.equalsIgnoreCase("accept")) {
                requestC.doAcceptRefund(request, response);
            } else if (actionRefund.equalsIgnoreCase("reject")) {
                requestC.doRejectRefund(request, response);
            }
        }
            ArrayList<Booking> bookings = requestC.getRequestBookings();
            request.setAttribute("bookings", bookings);
            return "/WEB-INF/jsp/refundManager.jsp";
        

    }

    private String releaseSeat(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        if (request.getParameter("screeningid") != null) {
            ticketBooking booking = new ticketBooking();
            booking.releaseTicket(request, response);
            return "/WEB-INF/jsp/seatRelease.jsp";
        } else {
            return this.doIndexPage(request, response);
        }

    }

    private String reserveSeat(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        if (request.getParameter("screeningid") != null) {
            ticketBooking booking = new ticketBooking();
            booking.reserveTicket(request, response);
            return "/WEB-INF/jsp/seatReserve.jsp";
        } else {
            return this.doIndexPage(request, response);
        }

    }

    private String doReserve(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        if (Integer.parseInt(request.getParameter("number")) != 0) {
            ticketBooking booking = new ticketBooking();
            booking.doReserveTicket(request, response);
            return "/WEB-INF/jsp/doSeatManager.jsp";
        } else {
            return this.doIndexPage(request, response);
        }

    }

    private String doRelease(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        if (Integer.parseInt(request.getParameter("number")) != 0) {
            ticketBooking booking = new ticketBooking();
            booking.doReleaseTicket(request, response);
            return "/WEB-INF/jsp/doSeatManager.jsp";
        } else {
            return this.doIndexPage(request, response);
        }

    }

    private String doManage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        return "/WEB-INF/jsp/manage.jsp";
    }

    private String doLogout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //invalidate the session
        HttpSession session = request.getSession();


        if (session.getAttribute("member") != null) {
            session.removeAttribute("member");
            session.removeAttribute("loggedin");
        }

        session.invalidate();

        //prepare for index page
        MovieDAO movieDAO = new MovieDAO();
        request.setAttribute("movies", movieDAO.getMovies());
        return "/index.jsp";

    }

    public static String doIndexPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        MovieDAO movieDAO = new MovieDAO();
        HttpSession session = request.getSession();
        if (session.getAttribute("member") != null) {
            request.setAttribute("member", session.getAttribute("member"));
        }

        request.setAttribute("movies", movieDAO.getMovies());
        
        return "/index.jsp";


        /*

        MovieDAO movieDAO = new MovieDAO();
        if (session == null) {
            session = request.getSession();
        }
        if (userLoggedin == false) {
            session.setAttribute("member", member);
        }
        request.setAttribute("movies", movieDAO.getMovies());
        request.setAttribute("member", session.getAttribute("member"));
*/


    }

    private String doSortMovie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        movieRetrieve retrieve = new movieRetrieve();
        retrieve.sortMovie(request, response);
        return "/index.jsp";
    }

    private String doSortManager(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        movieRetrieve retrieve = new movieRetrieve();
        retrieve.sortMovie(request, response);
        return "/WEB-INF/jsp/movieManager.jsp";
    }

    private String doSearchMovie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        movieRetrieve retrieve = new movieRetrieve();
        retrieve.searchMovie(request, response);
        return "/index.jsp";
    }

    private String doSearchManager(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        movieRetrieve retrieve = new movieRetrieve();
        retrieve.searchMovie(request, response);
        return "/WEB-INF/jsp/movieManager.jsp";
    }

    private String doMovieViewer(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        movieRetrieve retrieve = new movieRetrieve();
        retrieve.retrieveMovie(request, response);
        return "/WEB-INF/jsp/movieViewer.jsp";
    }

    private String bookTicket(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ticketBooking booking = new ticketBooking();
        booking.bookTicket(request, response);
        return "/WEB-INF/jsp/book.jsp";
    }

    private String doPay(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ticketBooking booking = new ticketBooking();
        if(request.getParameter("tickets").equals(""))
            return this.doIndexPage(request, response);
        booking.doPreBookTicket(request, response);
        return "/WEB-INF/jsp/payment.jsp";
    }

    private String doBookTicket(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ticketBooking booking = new ticketBooking();
        booking.doBookTicket(request, response);
        return "/WEB-INF/jsp/doBook.jsp";
    }

    private String doPrepareRegisterPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        return "/WEB-INF/jsp/registerOfficer.jsp";
    }

    private String doPrepareManagement(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String managePage = request.getParameter("managelist");

        if (managePage.equals("/WEB-INF/jsp/movieManager.jsp")) {

            MovieDAO movieDAO = new MovieDAO();
            request.setAttribute("movies", movieDAO.getMovies());
        } else if (managePage.equals("/WEB-INF/jsp/officerManager.jsp")) {
            MemberDAO memberDAO = new MemberDAO();
            request.setAttribute("officers", memberDAO.getOfficers());

        }else if (managePage.equals("/WEB-INF/jsp/refundManager.jsp")) {
            requestControll requestC = new requestControll();
            ArrayList<Booking> bookings = requestC.getRequestBookings();
            request.setAttribute("bookings", bookings);

        } else if (managePage.equals("/WEB-INF/jsp/seatManager.jsp")) {
            screeningControl screeningC = new screeningControl();
            ArrayList<Screening> screenings = screeningC.getScreenings();
            request.setAttribute("screenings", screenings);

        }else if (managePage.equals("/WEB-INF/jsp/statisticManager.jsp")) {
            HouseDAO houseDAO = new HouseDAO();
            request.setAttribute("houses", houseDAO.getHouseStatistics());
        }
        return managePage;
    }

    private String doRegister(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        memberControl register = new memberControl();
        return register.doRegisterPage(request, response);
    }

    private String doPrepareAddMoviePage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        return "/WEB-INF/jsp/movieAdder.jsp";

    }

    private String doAddMoviePage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        movieControll adder = new movieControll();
        return adder.doAddMoviePage(request, response);

    }

    private String doPrepareUpdateMoviePage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        movieControll updater = new movieControll();

        return updater.doPrepareUpdateMoviePage(request, response);

    }

    private String doUpdateMoviePage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        movieControll updater = new movieControll();

        return updater.doUpdateMoviePage(request, response);

    }

    private String doPrepareUpdateMemberPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        memberControl updater = new memberControl();

        return updater.doPrepareUpdateMemberPage(request, response);

    }

    private String doUpdateMemberPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        memberControl updater = new memberControl();

        return updater.doUpateMemberPage(request, response);

    }

    private String doPrepareDeleteMember(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        memberControl del = new memberControl();
        return del.doPrepareDeleteMemberPage(request, response);
    }

    private String doDeleteMember(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        memberControl del = new memberControl();
        return del.doDeleteMemberPage(request, response);
    }

    private String doPrepareDeleteMoviePage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        movieControll deleter = new movieControll();

        return deleter.doPrepareDeleteMoviePage(request, response);

    }

    private String doDeleteMoviePage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        movieControll deleter = new movieControll();

        return deleter.doDeleteMoviePage(request, response);

    }

    private String doMessageBox(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       String jspPage="";
        MessageControl messageC = new MessageControl();
        messageC.retrieveMessages(request, response);
        HttpSession session = request.getSession();
        Member member = (Member) session.getAttribute("member");
        if(member!=null)
            jspPage= "/messageBox.jsp";
        else
            jspPage= "/messageBox_nonMember.jsp";
        return jspPage;
    }

    private String doAddMessage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       String jspPage="";
        MessageControl messageC = new MessageControl();
        messageC.doAddMessage(request, response);
        messageC.retrieveMessages(request, response);
                HttpSession session = request.getSession();
        Member member = (Member) session.getAttribute("member");
        if(member!=null)
            jspPage= "/messageBox.jsp";
        else
            jspPage= "/messageBox_nonMember.jsp";
        return jspPage;
    }

        private String doDeleteMessage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       String jspPage="";
        MessageControl messageC = new MessageControl();
        messageC.doDeleteMessage(request, response);
        messageC.retrieveMessages(request, response);
                HttpSession session = request.getSession();
        Member member = (Member) session.getAttribute("member");
        if(member!=null)
            jspPage= "/messageBox.jsp";
        else
            jspPage= "/messageBox_nonMember.jsp";
        return jspPage;
    }
   private String doViewStatistics(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        statisticsViewer staView = new statisticsViewer();
        staView.doViewStatistics(request, response);
        return "/WEB-INF/jsp/statisticManager.jsp";
   }

   private String doUpdateUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        memberControl memberC=new memberControl();
        memberC.doChangePassword(request, response);

        return "/WEB-INF/jsp/manageMember.jsp";
   }

   private String doPrepareScreeningManagement(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       screeningControl sc = new screeningControl();
       return sc.doManageScreening(request, response);
   }

   private String doPrepareAddScreening(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       screeningControl sc = new screeningControl();
       return sc.doPrepareAddScreeningPage(request, response);
   }
   private String doAddScreening(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       screeningControl sc = new screeningControl();
       return sc.doAddScreeningPage(request, response);
    }

   private String doPrepareUpdateScreening(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       screeningControl sc = new screeningControl();
       return sc.doPrepareUpdateScreeningPage(request, response);
    }

   private String doUpdateScreening(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       screeningControl sc = new screeningControl();
       return sc.doUpdateScreeningPage(request, response);
    }

   private String doPrepareDeleteScreening(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       screeningControl sc = new screeningControl();
       return sc.doPrepareDeleteScreeningPage(request, response);
    }

   private String doDeleteScreening(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       screeningControl sc = new screeningControl();
       return sc.doDeleteScreeningPage(request, response);
    }
   
}
